<?php

declare(strict_types=1);

namespace DreamCat\ControllerParamGen4Laravel\Provider;

use Dreamcat\Class2Array\JsonValueFixer;
use DreamCat\ControllerParamGen\ControllerParamGeneratorInterface;
use DreamCat\ControllerParamGen4Laravel\Dispatcher\AnnotationCtlDispatcher;
use DreamCat\ControllerParamGen4Laravel\Factory\CtlParamGenFactory;
use DreamCat\ControllerParamGen4Laravel\Factory\JsonDecodeFactory;
use DreamCat\ControllerParamGen4Laravel\Factory\LaravelRequestFactory;
use Illuminate\Http\Request;
use Illuminate\Routing\Contracts\ControllerDispatcher;
use Illuminate\Support\ServiceProvider;
use Psr\Http\Message\RequestInterface;

/**
 * 控制器参数相关的提供器
 * @author vijay
 */
class CtlParamGenProvider extends ServiceProvider
{
    /**
     * @inheritDoc
     * @noinspection PhpMissingParentCallCommonInspection
     */
    public function register()
    {
        $this->app->singleton(
            ControllerParamGeneratorInterface::class,
            function () {
                return $this->app->make(CtlParamGenFactory::class)->factory();
            }
        );
        $this->app->singleton(ControllerDispatcher::class, AnnotationCtlDispatcher::class);
        $this->app->bind(RequestInterface::class, function () {
            $request = $this->app->make(Request::class);
            return $this->app
                ->make(LaravelRequestFactory::class)
                ->create($request);
        });
        $this->app->singleton(JsonValueFixer::class, function () {
            return $this->app
                ->make(JsonDecodeFactory::class)
                ->createFixer();
        });
    }
}

# end of file
